//
//  LBTableViewCell.m
//  MyProject
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBTableViewCell.h"

#import "LBCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@interface LBTableViewCell ()

@property (nonatomic,retain) NSTimer *timer;



@end
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)
@implementation LBTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
             self.item_urlArray = [NSMutableArray array];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(375 * WIDTH, 300 * HEIGH);
        //        //竖向
        //        flowLayout.minimumInteritemSpacing = 0;
        //横向间距
        flowLayout.minimumLineSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
        [_collectionView registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:@"hi"];
//        _collectionView.backgroundColor = [UIColor redColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
   
        
    }
    return  self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.bounds;
    
}
- (void)setPicArray:(NSMutableArray *)bgpicArray
{
    _bgpicArray = bgpicArray;
}
- (void)setTitleArray:(NSMutableArray *)titleArray
{
    _titleArray = titleArray;
}
- (void)setCommenImageArray:(NSMutableArray *)commenImageArray
{
    _commenImageArray = commenImageArray;
}
- (void)setCommenTitleArray:(NSMutableArray *)commenTitleArray
{
    _commenTitleArray = commenTitleArray;
}
- (void)setDescribArray:(NSMutableArray *)describArray
{
    _describArray = describArray;
}

- (void)updateTimer
{
    NSInteger contentX = _collectionView.contentOffset.x;
    if (contentX == self.contentView.bounds.size.width * (_bgpicArray.count - 1)) {
        _collectionView.contentOffset = CGPointMake(0, 0);
    }else{
        [_collectionView setContentOffset:CGPointMake(contentX + self.contentView.bounds.size.width, 0) animated:YES];
        
    }
    CATransition *transtion = [CATransition animation];
    transtion.type = @"kCATransitionMoveIn";
    transtion.duration = 3;
    [_collectionView.layer addAnimation:transtion forKey:@"ee"];
}
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _bgpicArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hi" forIndexPath:indexPath];
    [cell.bgimage sd_setImageWithURL:[NSURL URLWithString:[_bgpicArray objectAtIndex:indexPath.item]]];
    cell.titleLabel.text = [_titleArray objectAtIndex:indexPath.item];
    [cell.commenImage sd_setImageWithURL:[NSURL URLWithString:[_commenImageArray objectAtIndex:indexPath.item]]];
    cell.commenLabel.text = [_commenTitleArray objectAtIndex:indexPath.item];
    cell.decribLabel.text = [_describArray objectAtIndex:indexPath.item];
    
    
    return cell;
}
//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.delegate passValue:[_idArray objectAtIndex:indexPath.item]];
    [self.delegate passValue:[self.item_urlArray objectAtIndex:indexPath.item]];
    NSLog(@"%@",[self.item_urlArray objectAtIndex:indexPath.item]);
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
